﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections;
using HasuSLLib;
using HasuSLUI.Utility;
using System.Windows.Data;

namespace HasuSLUI
{
    public static  class UIGenerator
    {
        #region RegisterdControls collection

        private static Dictionary<Type, Type> _registerdControls = new Dictionary<Type, Type>();

        /// <summary>
        /// RegisterdControls collection
        /// </summary>
        public static Dictionary<Type, Type> RegisterdControls
        {
            get { return _registerdControls; }
            set { _registerdControls = value; }
        }

        #endregion

        #region Static Constructor
        /// <summary>
        /// use for register control and type
        /// </summary>
        static UIGenerator()
        {

            RegisterdControls.Add(typeof(string), typeof(TextBox));
            RegisterdControls.Add(typeof(int), typeof(TextBox));
            RegisterdControls.Add(typeof(Int16), typeof(TextBox));
            RegisterdControls.Add(typeof(long), typeof(TextBox));
            RegisterdControls.Add(typeof(double), typeof(TextBox));
            RegisterdControls.Add(typeof(float), typeof(TextBox));

            RegisterdControls.Add(typeof(bool), typeof(CheckBox));
            //RegisterdControls.Add(typeof(TripleState), typeof(TripleStateControl));

            RegisterdControls.Add(typeof(DateTime), typeof(DatePicker));
            RegisterdControls.Add(typeof(Date), typeof(DatePicker));
            //RegisterdControls.Add(typeof(Time), typeof(TimePicker));

            //RegisterdControls.Add(typeof(HasuUIGen2.Lib.Interfaces.ILookup), typeof(ComboBox));
            RegisterdControls.Add(typeof(System.Collections.IEnumerable), typeof(ListBox));
            RegisterdControls.Add(typeof(System.Collections.ICollection), typeof(MetaGrid));
            RegisterdControls.Add(typeof(System.Enum), typeof(ComboBox));

        }

        #endregion

        #region IsRegister
        /// <summary>
        /// to check wather control IsRegisterdControl
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static bool IsRegisterdControl(Type type)
        {
            return RegisterdControls.ContainsKey(type);
        }
        #endregion

        #region CreateControl


        public static Control CreateControl(Metadata metadata)
        {
            Control ctrl = null;
            if (metadata == null)
                return ctrl;

            if (IsRegisterdControl(metadata.MetaType))
            {
                ctrl = (Control)Activator.CreateInstance(RegisterdControls[metadata.MetaType]);
            }
            else
            {
                if (metadata.MetaType.IsClass && Activator.CreateInstance(metadata.MetaType) is System.Collections.IEnumerable)
                {
                    if (metadata.MetaType.IsGenericType)
                    {
                        if (metadata.MetaType.GetGenericArguments().Length == 1 && IsRegisterdControl(metadata.MetaType.GetGenericArguments()[0]))
                        {
                            ctrl = (Control)Activator.CreateInstance(RegisterdControls[typeof(System.Collections.IEnumerable)]);
                        }
                        else
                        {
                            ctrl = (Control)Activator.CreateInstance(RegisterdControls[typeof(System.Collections.ICollection)]);
                        }
                    }
                    else if (Activator.CreateInstance(metadata.MetaType) is System.Collections.ICollection)
                        ctrl = (Control)Activator.CreateInstance(RegisterdControls[typeof(System.Collections.ICollection)]);
                    else
                        ctrl = (Control)Activator.CreateInstance(RegisterdControls[typeof(System.Collections.IEnumerable)]);

                }
                else if (metadata.MetaType.IsEnum)
                {
                    ctrl = (Control)Activator.CreateInstance(RegisterdControls[typeof(System.Enum)]);
                }
            }

            return ctrl;
        }
        #endregion


        #region Data binding

        public delegate void BindingHandler(object sender, BindingEventArgs e);

        public static event BindingHandler OnBinding;

        public static void SetBinding(this Control control, Metadata metadata, object entity)
        {
            Binding bind = new Binding(metadata.Name );
            bind.Source = entity;
            if(control is TextBox)
            {control.SetBinding(TextBox.TextProperty, bind);}
            else if (control is CheckBox)
            { control.SetBinding(CheckBox.IsCheckedProperty, bind); }
            else if (control is DatePicker)
            { control.SetBinding(DatePicker.SelectedDateProperty, bind); }
            else if (control is ListBox)
            { control.SetBinding(ListBox.ItemsSourceProperty, bind); }
            else if (control is ComboBox)
            { control.SetBinding(ComboBox.ItemsSourceProperty, bind); }
            else
            {
                if (OnBinding != null)
                {
                    OnBinding(control, new BindingEventArgs() { Entity = entity, Metadata = metadata });
                }
            }
        }


        #endregion

        #region Control Event
        /*
        public static void SetEventAndValue(Control control, Metadata metadata)
        {
            if (control is TextBox)
            {
                object value = metadata.Value;
                (control as TextBox).Text = value == null ? string.Empty : value.ToString();

                (control as TextBox).TextChanged += new TextChangedEventHandler(UIGenerator_ValueChanged);
            }
            else if (control is CheckBox)
            {
                object value = metadata.Value;
                (control as CheckBox).IsChecked = (bool)value;

                (control as CheckBox).Checked += new RoutedEventHandler(UIGenerator_CheckBoxChanged);
                (control as CheckBox).Unchecked += new RoutedEventHandler(UIGenerator_CheckBoxChanged);
            }
            else if (control is DatePicker)
            {
                object value = metadata.Value;
                (control as DatePicker).SelectedDate = (DateTime)value;
                (control as DatePicker).SelectedDateChanged += new EventHandler<SelectionChangedEventArgs>(UIGenerator_DateTimeChanged);
            }
            else if (control is ListBox)
            {
                (control as ListBox).Height = 100;
                object value = metadata.Value;
                (control as ListBox).ItemsSource = (IEnumerable)value;
            }
            else if (control is ComboBox && metadata.MetaType.IsEnum)
            {
                (control as ComboBox).SelectionChanged += new SelectionChangedEventHandler(UIGenerator_SelectionChanged);
                //(control as ComboBox).ItemsSource = Enum.GetNames(metadata.MetaType);

                (control as ComboBox).ItemsSource = OtherExtra.GetNames(metadata.MetaType);
                if (metadata.Value != null)
                    (control as ComboBox).SelectedItem = metadata.Value;
            }
            else if (control is MetaGrid)
            {
                object value = metadata.Value;
                (control as MetaGrid).EntityCollection = (value as ICollection);
            }
        }

        static void UIGenerator_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //((sender as ComboBox).Tag as Metadata).Value = Convert.ChangeType((sender as ComboBox).SelectedItem, ((sender as ComboBox).Tag as Metadata).MetaType, null);   
            ((sender as ComboBox).Tag as Metadata).Value = Enum.Parse(((sender as ComboBox).Tag as Metadata).MetaType, (sender as ComboBox).SelectedItem.ToString(), true);
        }

        static void UIGenerator_DateTimeChanged(object sender, SelectionChangedEventArgs e)
        {
            ((sender as DatePicker).Tag as Metadata).Value = (sender as DatePicker).SelectedDate;
        }

        static void UIGenerator_CheckBoxChanged(object sender, RoutedEventArgs e)
        {
            ((sender as CheckBox).Tag as Metadata).Value = (sender as CheckBox).IsChecked;
        }

        static void UIGenerator_ValueChanged(object sender, TextChangedEventArgs e)
        {
            ((sender as TextBox).Tag as Metadata).Value = Convert.ChangeType((sender as TextBox).Text, ((sender as TextBox).Tag as Metadata).MetaType, null);
        }

         *    */

        #endregion

        #region SetToolTip

        public static void SetToolTip(Control control, Metadata metadata)
        {
            if (metadata.LocationAndSize != null && !string.IsNullOrEmpty(metadata.LocationAndSize.ToolTip))
            {
                control.MouseEnter += new MouseEventHandler(control_MouseEnter);
            }
        }

        static void control_MouseEnter(object sender, MouseEventArgs e)
        {

            return; //due to seme error in tooltip service
            /*
             * 
            ToolTip tooltip = new ToolTip();

            tooltip.Content = new TextBlock()
            {

                //FontFamily = new FontFamily("Georgia"), FontSize = 14,

                Text = ((sender as Control).Tag as Metadata ).LocationAndSize.ToolTip ,
                TextWrapping = TextWrapping.Wrap
            };
             * 
             */

            ToolTipService.SetToolTip((Control)sender, ((sender as Control).Tag as Metadata).LocationAndSize.ToolTip);

        }
        #endregion

    }
    
}
